/*
 * Created on 14.01.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.usr.messaging.AlertDB;
import ro.gateway.aida.utils.Utils;


/**
 * @author Mihai Postelnicu<p>
 *
 * e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 * Implements alert diplaying
 * modes = widget, list, view
 */
public class AlertAction extends MultiTilesAction {
    private AlertDB adb;
    private Arbiter ar;

    public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        adb = AlertDB.getManager(token);
        ar = Arbiter.getManager(token);

        User user = UserUtils.getLogged(request, response);

        if (user == null)
            return mapping.findForward("norights");

        return modeSelect(mapping, form, request, response);
    }

    public ActionForward modeWidget(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
        request.setAttribute("alerts", adb.getForUser(user.getId()));
        return mapping.findForward("widget");
    }

    public ActionForward modeList(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
        request.setAttribute("alerts", adb.getForUser(user.getId()));
		Utils.setBreadCrumbs(request,"bc.alert.list");
        return mapping.findForward("list");
    }

    public ActionForward modeDelete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        String[] delalerts = request.getParameterValues("delalerts");

        if (delalerts != null)
            for (int i = 0; i < delalerts.length; i++)
                adb.markDeleted(Long.parseLong(Utils.getStrMapAttr(
                            delalerts[i], "user_id")),
                    Long.parseLong(Utils.getStrMapAttr(delalerts[i], "event_id")));

        return modeList(mapping, form, request, response);
    }

    public ActionForward modeView(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
        String user_id = request.getParameter("user_id");
        String event_id = request.getParameter("event_id");
        request.setAttribute("alert",
            adb.getByIds(Long.parseLong(user_id), Long.parseLong(event_id)));
        adb.markRead(Long.parseLong(user_id), Long.parseLong(event_id));
		Utils.setBreadCrumbs(request,"bc.alert.view");
        return mapping.findForward("view");
    }



    public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        String mode = request.getParameter("mode");

        if (mode == null)
            mode = (String) request.getAttribute("mode");

        if ("widget".equals(mode))
            return modeWidget(mapping, form, request, response);

        if ("view".equals(mode))
            return modeView(mapping, form, request, response);

        if ("delete".equals(mode))
            return modeDelete(mapping, form, request, response);

        return modeList(mapping, form, request, response);
    }
}
